package cn.edu.ujn.finallab.controller;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import cn.edu.ujn.finallab.dao.Admin;
import cn.edu.ujn.finallab.dao.Customer;
import cn.edu.ujn.finallab.service.IAdminService;
import cn.edu.ujn.finallab.util.MD5Encoder;

@Controller
public class AdminController {

	@Autowired
	private IAdminService adminService;

	// 跳转到管理员界面
	@RequestMapping("/adminLogin")
	public String adminLogin() {
		return "adminLogin";
	}

	// 管理员提交登录信息
	@RequestMapping("/adminLoginSubmit")
	public String adminLoginSubmit(String adminname, String password, Model model, HttpSession session) {
		Admin admin = new Admin(0, adminname, password);
		session.setAttribute("admin", admin);
		if ("".equals(adminname)) {
			model.addAttribute("msg", "用户名为空！");
			return "adminLogin";
		} else if ("".equals(password)) {
			model.addAttribute("msg", "密码为空！");
			return "adminLogin";
		}
		// 根据输入的adminname查询数据库，判断该adminname是否存在
		// 若存在，对密码进行加密，再判断加密结果是否相等，都相等时跳转到管理员主界面，否则返回到登录界面提示相应信息
		Admin adminSelected = this.adminService.selectAdminByName(adminname);
		// 没有查询到该用户的信息
		if (adminSelected == null) {
			model.addAttribute("msg", "未查询到您的信息！");
			return "adminLogin";
		} else {
			// 加密判断
			String result = "";
			MD5Encoder pe = new MD5Encoder();
			try {
				result = pe.geMd5(adminname, password);
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (!adminSelected.getPassword().equals(result)) {
				model.addAttribute("msg", "密码错误");
				return "adminLogin";
			}
			session.setAttribute("admin", adminSelected);
			model.addAttribute("admin", admin);
			return "indexOfAdmin"; // 跳转到管理员主页
		}
	}

	// 退出时注销session
	@RequestMapping("/adminExit")
	public String adminExit(HttpSession session) {
		session.invalidate();
		return "adminLogin";
	}
	
	// 返回到管理员主页
	@RequestMapping("return")
	public String returnIndexOfAdmin() {
		return "indexOfAdmin";
	}

	//添加管理员
	@RequestMapping("addAdmin")
	public String addAdmin(String name,String password,Model model) {
		int id = 0;
		String result = "";
		Admin selectAdminByName = this.adminService.selectAdminByName(name);
		if(selectAdminByName!=null) {
			model.addAttribute("msg", "用户已存在");
			return "addAdmin";
		}
		MD5Encoder pe = new MD5Encoder();
		try {
			result = pe.geMd5(name, password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Admin admin = new Admin(id,name,result);
		admin.setId(id);
		admin.setName(name);
		admin.setPassword(result);
		int insert = this.adminService.insertAdmin(admin);
		if(insert > 0) {
			model.addAttribute("msg", "添加成功");
			System.out.println("插入成功！");
			return "addAdmin";
		} else {
			System.out.println("插入失败！");
			return "addAdmin";
		}
	}
	
	// 跳转到添加管理员界面
	@RequestMapping("toAddAdmin")
	public String toAddAdmin() {
		return "addAdmin";
	}
	//管理员修改密码
	@RequestMapping("/changePassword")
	public String changePassword(String password,String newpassword1,String newpassword2,Model model,HttpSession session) {
		MD5Encoder md5Encoder = new MD5Encoder();
		Admin admin = (Admin) session.getAttribute("admin");
		System.out.println("session" + admin);
		Admin admin2 = this.adminService.selectAdminByName(admin.getName());
		System.out.println(admin2);
		String result="";
		try {
			result = md5Encoder.geMd5(admin.getName(), password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		model.addAttribute("msg","");
		if(!admin2.getPassword().equals(result)) {
			model.addAttribute("msg","请输入正确的原密码后进行修改");
			return "changePassword";
		}else if(!(newpassword1.equals(newpassword2))) {
			model.addAttribute("msg","两次密码输入不一致");
			return "changePassword";
		}
		String newpassword="";
		try {
			newpassword = md5Encoder.geMd5(admin.getName(), newpassword1);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		admin2.setPassword(newpassword);
		int updateAdmin = this.adminService.updateAdmin(admin2);
		model.addAttribute("msg","修改成功，请重新登录");
		session.invalidate();
		return "adminLogin";
	}
}
